import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:it_cat/Tools/tools_base_page.dart';

class HomeLocalRefreshPage extends CatBaseWidgetPage {
  @override
  _HomeLocalRefreshPageState getState() => _HomeLocalRefreshPageState();
}

class _HomeLocalRefreshPageState extends CatBaseWidgetPageState<HomeLocalRefreshPage> {

  GlobalKey<_TextWidgetState> textKey = GlobalKey();

  int _count = 0;

  @override
  Widget buildWidget(BuildContext context) {
   return Container(
     child: Column(
       // mainAxisAlignment: MainAxisAlignment.center,
       crossAxisAlignment: CrossAxisAlignment.center,
       children: <Widget>[
         TextWidget(textKey),
         FlatButton(child: Text('$_count'), onPressed: (){
           _count++;
           textKey.currentState.onPressed(_count);  //其实这个count值已经改变了 但是没有重绘所以我们看到的只是我们定义的初始值
         },),
       ],
     ),
   );

  }




}

//封装的widget
class TextWidget extends StatefulWidget {
  final Key key;

  const TextWidget(this.key);

  @override
  _TextWidgetState createState() => _TextWidgetState();
}

class _TextWidgetState extends State<TextWidget> {
  String text = "0";

  void onPressed(int count) {
    setState((){
      text = count.toString();
    });
  }

  @override
  Widget build(BuildContext context) {
    return new Text(text);
  }
}
